package com.guopiao.vat.common.util;

import sun.misc.BASE64Decoder;

import java.io.UnsupportedEncodingException;


/**
 * base64 编码、解码util
 *
 * @author lifq
 * @date 2015-3-4 上午09:23:33
 */
public class Base64Util {

  /**
   * 将 s 进行 BASE64 编码
   *
   * @return String
   * @author lifq
   * @date 2015-3-4 上午09:24:02
   */
  public static String encode(String s) {
    if (s == null) {
      return null;
    }
    String res = "";
    try {
      res = new sun.misc.BASE64Encoder().encode(s.getBytes("GBK"));
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return res;
  }

  /**
   * 将 BASE64 编码的字符串 s 进行解码
   *
   * @return String
   * @author lifq
   * @date 2015-3-4 上午09:24:26
   */
  public static String decode(String s) {
    if (s == null) {
      return null;
    }
    BASE64Decoder decoder = new BASE64Decoder();
    try {
      byte[] b = decoder.decodeBuffer(s);
      return new String(b, "GB2312");
    } catch (Exception e) {
      return null;
    }
  }

  /**
   * @return void
   * @author lifq
   * @date 2015-3-4 上午09:23:17
   */
  public static void main(String[] args) {
    System.out.println(Base64Util.encode("哈哈"));
    System.out.println(Base64Util.decode(
        "eyJhbGciOiJIUzUxMiJ9.eyJyYW5kb21LZXkiOiIydHNrdGkiLCJzdWIiOiJhZG1pbiIsImZ1bGxfbmFtZSI6IuW_q-S5kOicgiIsInVzZXJfaWQiOjEsInVzZXJfbmFtZSI6ImFkbWluIiwiZXhwIjoxNTE3NDAzMDIyLCJpYXQiOjE1MTY3OTgyMjJ9.6p_osTD99ZqPqFQ4XQQgmr1tn6vHJz0cyyREA_Ib0ZvvPsP7nCG2uuEbpJglTEIZRAP3C2rxWjDAPvAWS_15Yg"));

  }

}
